﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface ISCJHMXBDAL : IDAL<SCJHMXB>
    {
        List<XSHTMXBModel> GetSCJHMXB(Guid JHID, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }

    public class SCJHMXBDAL : BaseDAL<SCJHMXB>, ISCJHMXBDAL
    {
        public SCJHMXBDAL(IUnitOfWork context)
            : base(context)
        {
        }

        public List<XSHTMXBModel> GetSCJHMXB(Guid JHID, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from mxb in dataContext.SCJHMXB.Where(t => t.JHID == JHID)
                         join htmx in dataContext.XSHTMXB.Include("XSHT") on mxb.HTMXBID equals htmx.ID
                         join c in dataContext.vCP on htmx.CPID equals c.ID
                         join k in dataContext.ZL_KH on htmx.XSHT.KHID equals k.ID
                         select new XSHTMXBModel
                         {
                             ID = mxb.ID,
                             XSHTID = htmx.XSHTID,
                             HTZT = htmx.XSHT.HTZT,
                             HTBH = htmx.XSHT.BH,
                             KHJC = k.JIANCHENG,
                             DHSJ = htmx.XSHT.DHSJ,
                             CHSJ = htmx.XSHT.CHSJ,
                             CPID = htmx.CPID,
                             CPBH = c.BH,
                             CPMC = c.CPMC,
                             CPGG = c.CPGG,
                             CPYS = c.YS,
                             CPLX = c.CPLXMC,
                             SL = htmx.SL,
                             DJ = htmx.DJ
                         };
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
